package com.gopay.bis.ccb.ebank.operation;

import java.util.HashMap;
import java.util.Map;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.ccb.ebank.query.res.LIST;
import com.gopay.common.domain.bank.ccb.ebank.query.res.TX;
import com.gopay.common.domain.bank.ccb.ebank.query.res.TX_INFO;
import com.gopay.common.domain.bank.ccb.ebank.pay.CcbB2bMessage;
import com.gopay.common.exception.GopayException;

/**
 * 解析建行B2B返回报文xml
 * 
 * @CcbBuildMessage.java
 * @author WangHongFei
 */
public class CcbB2bParseMessage {

	/**
	 * 解析B2B查询返回报文格式xml，返回object
	 * 
	 * @param Object
	 * @return CCBMessage
	 */
	public CcbB2bMessage parseB2bXml(Object receivedMsgData) throws GopayException{
		CcbB2bMessage ccbMsg = new CcbB2bMessage();
		ccbMsg.setOutStlmCd(BankConstants.BANK_LOGO_CCB);
		try {
			String xmlMsg = (String) receivedMsgData;
			@SuppressWarnings("rawtypes")
			Map<String, Class> cm = new HashMap<String, Class>();
			cm.put("TX", TX.class);
			cm.put("TX_INFO", TX_INFO.class);
			cm.put("LIST", LIST.class);
			TX tx = (TX) XMLBeanUtils.xml2Bean(cm, xmlMsg);
			ccbMsg = getCcbB2bMessage(tx);
			return ccbMsg;
		} catch (Exception e) {
			BankLogUtils.printB2BQueryExceptionLog(ccbMsg, e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6038);
		}
	}

	/**
	 * 解析Tx
	 * 
	 * @param TX
	 * @return CcbB2bMessage
	 */
	private CcbB2bMessage getCcbB2bMessage(TX tx) {
		CcbB2bMessage ccbMsg = new CcbB2bMessage();
		ccbMsg.setRequestSn(tx.getREQUEST_SN());
		ccbMsg.setCustId(tx.getCUST_ID());
		ccbMsg.setTxCode(tx.getTX_CODE());
		ccbMsg.setReturnCode(tx.getRETURN_CODE());
		ccbMsg.setReturnMsg(tx.getRETURN_MSG());
		ccbMsg.setLanguage(tx.getLANGUAGE());
		if (tx.getTX_INFO() != null) {
			ccbMsg.setCurPage(tx.getTX_INFO().getCUR_PAGE());
			ccbMsg.setPageCount(tx.getTX_INFO().getPAGE_COUNT());
			ccbMsg.setTranDate(tx.getTX_INFO().getLIST().getTRAN_DATE());
			ccbMsg.setAccDate(tx.getTX_INFO().getLIST().getACC_DATE());
			ccbMsg.setOrder(tx.getTX_INFO().getLIST().getORDER());
			ccbMsg.setAccount(tx.getTX_INFO().getLIST().getACCOUNT());
			ccbMsg.setPaymentMoney(tx.getTX_INFO().getLIST().getPAYMENT_MONEY());
			ccbMsg.setRefundMoney(tx.getTX_INFO().getLIST().getREFUND_MONEY());
			ccbMsg.setPostId(tx.getTX_INFO().getLIST().getPOS_ID());
			ccbMsg.setRemark1(tx.getTX_INFO().getLIST().getREM1());
			ccbMsg.setRemark2(tx.getTX_INFO().getLIST().getREM2());
			ccbMsg.setStatus(tx.getTX_INFO().getLIST().getORDER_STATUS());
		}
		return ccbMsg;
	}

}
